package com.inossem.warehousestorage_inossem.utils;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.inossem.utils.base.ArithUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.adapter.OverviewAdapter;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMaterielConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMaterielIndexBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManagePositionConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageMaterielBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/23 0023.
 */

public class StorageManageUtils {
//    public static double getUpCount(StorageManageMatrBean information) {
//        double ret = 0;
//        for (StorageManageMatrBean.DataBean.MaterielListBean m : information.getData().getMaterielList()) {
//            ret = ret + getUpCount(m);
//        }
//        return ret;
//    }
//
//    public static StorageManageMatrCommitBean getCommitBean(StorageManageMatrBean information) {
//        List<StorageManageMatrBean.DataBean.MaterielListBean> ml = information.getData().getMaterielList();
//        StorageManageMatrCommitBean ret = new StorageManageMatrCommitBean();
//        for (StorageManageMatrBean.DataBean.MaterielListBean m : ml) {
//            List<UpFrameItemBean.DataBean.ListBean> ul = m.getUpList();
//            if (ul.size() > 0) {
//                StorageManageMatrCommitBean.DataBean.MaterielListBean rm = new StorageManageMatrCommitBean.DataBean.MaterielListBean();
//                rm.setSendPosition(m.getSendPosition());
//                rm.setBatchCode(m.getBatchCode());
//                rm.setMaterielCode(m.getMaterielCode());
//                rm.setLqnum(m.getLqnum());
//                ret.getData().getMaterielList().add(rm);
//                for (UpFrameItemBean.DataBean.ListBean u : ul) {
//                    StorageManageMatrCommitBean.DataBean.MaterielListBean.ReceivePositionListBean p = new StorageManageMatrCommitBean.DataBean.MaterielListBean.ReceivePositionListBean();
//                    p.setReceivePosition(u.getPosition());
//                    p.setNumber(u.getNumber());
//                    rm.getReceivePositionList().add(p);
//                }
//            }
//
//        }
//        return ret;
//    }
//
//    public static double getUpCount(StorageManageMatrBean.DataBean.MaterielListBean item) {
//        double result = 0D;
//        double[] numbers = new double[item.getUpList().size()];
//        for (int i = 0; i < item.getUpList().size(); i++) {
//            numbers[i] = Double.parseDouble(item.getUpList().get(i).getNumber());
//        }
//        result = ArithUtils.add(numbers);
//        return result;
//    }
//
//    public static List<OverviewAdapter.OverviewBean> getOverViewList(StorageManageMatrBean bean) {
//        List<OverviewAdapter.OverviewBean> resultList = new ArrayList<>();
//        for (int i = 0; i < bean.getData().getMaterielList().size(); i++) {
//            StorageManageMatrBean.DataBean.MaterielListBean materielItem = bean.getData().getMaterielList().get(i);
//            double upCount = getUpCount(materielItem);
//            if (upCount > 0) {
//                OverviewAdapter.OverviewBean overItem = new OverviewAdapter.OverviewBean();
//                overItem.setInfo(materielItem.getMaterielDescription());
//                overItem.setNumber(upCount + materielItem.getUnit());
//                resultList.add(overItem);
//            }
//        }
//        return resultList;
//    }
//
//    //物料整理删除行项目
//    public static void deleteMaterielItem(Context context, List<StorageManageMatrBean.DataBean.MaterielListBean> list, String materielCode, String batchCode, String sendPosition) {
//        StorageManageMatrBean bean = SPUtils.getInformation(context, StorageManageMatrBean.class, SPUtils.KEY_STORAGE_MGR_MATR_INFORMATION);
//        List<StorageManageMatrBean.DataBean.MaterielListBean> materielList = bean.getData().getMaterielList();
//        for (int i = 0; i < materielList.size(); i++) {
//            StorageManageMatrBean.DataBean.MaterielListBean materielItem = materielList.get(i);
//            if (materielItem.getMaterielCode().equals(materielCode) && materielItem.getBatchCode().equals(batchCode) && materielItem.getSendPosition().equals(sendPosition)) {
//                materielList.remove(i);
//                break;
//            }
//        }
//        bean.getData().setMaterielList(materielList);
//        SPUtils.saveInformation(context, SPUtils.KEY_STORAGE_MGR_MATR_INFORMATION, bean);
//        for (int i = 0; i < list.size(); i++) {
//            StorageManageMatrBean.DataBean.MaterielListBean materielLocalItem = list.get(i);
//            if (materielLocalItem.getMaterielCode().equals(materielCode) && materielLocalItem.getBatchCode().equals(batchCode) && materielLocalItem.getSendPosition().equals(sendPosition)) {
//                list.remove(i);
//                break;
//            }
//        }
//    }

    public static StorageManagePositionConfirmRequestBean getStorageManagePositionConfirmRequestBean(Context context, String factoryId, String inventoryAddressId, String warehouseId, String sendPosition, String targetPosition) {
        StorageManagePositionConfirmRequestBean result = new StorageManagePositionConfirmRequestBean();
        result.setFty_id(factoryId);
        result.setLocation_id(inventoryAddressId);
        result.setWh_id(warehouseId);
        result.setS_area_position_code(sendPosition);
        result.setT_area_position_code(targetPosition);
        return result;
    }

    //获取仓库整理基于物料整理的整理数量
    public static String getStorageMaterielCollateNumber(StorageManageMatrBean.BodyBean item) {
        String result = "0";
        List<StorageManageMatrBean.BodyBean.PositionBean> positionList = item.getStoragePositionList();
        if (positionList != null && positionList.size() > 0) {
            for (int i = 0; i < positionList.size(); i++) {
                result = ArithUtils.add(result, positionList.get(i).getNumber()).toString();
            }
        }
        return result;
    }

    public static List<StorageManageMatrBean.BodyBean> AddMateriel(List<StorageManageMatrBean.BodyBean> allMaterielList, List<StorageManageMatrBean.BodyBean> currentMaterielList) {
        if (currentMaterielList == null) {
            return allMaterielList;
        }
        List<StorageManageMatrBean.BodyBean> temp = new ArrayList<>(currentMaterielList);
        temp.retainAll(allMaterielList);
        currentMaterielList.removeAll(temp);
        allMaterielList.addAll(currentMaterielList);
        return allMaterielList;
    }

    //基于仓库整理保存信息
    public static void saveStorageMaterielBean(Context context, StorageMaterielBean bean) {
        Gson gson = new Gson();
        SPUtils.saveStorageMaterielInformation(context, gson.toJson(bean, StorageMaterielBean.class));
    }

    //基于仓库整理获取信息
    public static StorageMaterielBean getStorageMaterielBean(Context context) {
        String json = SPUtils.getStorageMaterielInformation(context);
        if (TextUtils.isEmpty(json)) {
            return new StorageMaterielBean();
        } else {
            Gson gson = new Gson();
            return gson.fromJson(json, StorageMaterielBean.class);
        }
    }

    public static void updateFactoryList(Context context, List<StorageManageMaterielIndexBean.BodyBean> factoryList) {
        StorageMaterielBean bean = getStorageMaterielBean(context);
        bean.setFactoryList(factoryList);
        saveStorageMaterielBean(context, bean);
    }

    public static void updateMaterielList(Context context, List<StorageManageMatrBean.BodyBean> materielList) {
        StorageMaterielBean bean = getStorageMaterielBean(context);
        bean.setMaterielList(materielList);
        saveStorageMaterielBean(context, bean);
    }

    public static void updateFactoryInfo(Context context, String factoryId, String factoryCode, String factoryName) {
        StorageMaterielBean bean = getStorageMaterielBean(context);
        bean.setFactoryId(factoryId);
        bean.setFactoryCode(factoryCode);
        bean.setFactoryName(factoryName);
        bean.setInventoryAddressId("");
        bean.setInventoryAddressCode("");
        bean.setInventoryAddressName("");
        bean.setWarehouseId("");
        saveStorageMaterielBean(context, bean);
    }

    public static void updateInventoryAddressInfo(Context context, String inventoryAddressId, String inventoryAddressCode, String inventoryAddressName, String warehouseId) {
        StorageMaterielBean bean = getStorageMaterielBean(context);
        bean.setInventoryAddressId(inventoryAddressId);
        bean.setInventoryAddressCode(inventoryAddressCode);
        bean.setInventoryAddressName(inventoryAddressName);
        bean.setWarehouseId(warehouseId);
        saveStorageMaterielBean(context, bean);
    }

    public static void deleteMateriel(Context context, String materielCode, String position, String batch) {
        StorageMaterielBean bean = getStorageMaterielBean(context);
        List<StorageManageMatrBean.BodyBean> materielList = bean.getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            StorageManageMatrBean.BodyBean materielItem = materielList.get(i);
            if (materielItem.getMat_code().equals(materielCode) && materielItem.getS_position_code().equals(position) && materielItem.getBatch().equals(batch)) {
                materielList.remove(materielItem);
                break;
            }
        }
        bean.setMaterielList(materielList);
        saveStorageMaterielBean(context, bean);
    }

    public static int getStorageMaterielConfirmNumber(Context context) {
        int result = 0;
        List<StorageManageMatrBean.BodyBean> materielList = getStorageMaterielBean(context).getMaterielList();
        if (materielList == null) {
            return result;
        } else {
            for (int i = 0; i < materielList.size(); i++) {
                StorageManageMatrBean.BodyBean materielItem = materielList.get(i);
                if (materielItem.getStoragePositionList() != null && materielItem.getStoragePositionList().size() > 0) {
                    result = result + 1;
                }
            }
        }
        return result;
    }

    public static StorageManageMatrBean.BodyBean getMaterielItemById(Context context, String id) {
        List<StorageManageMatrBean.BodyBean> materielList = getStorageMaterielBean(context).getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            StorageManageMatrBean.BodyBean materielItem = materielList.get(i);
            if (materielItem.getId().equals(id)) {
                return materielItem;
            }
        }
        return null;
    }

    public static String checkPositionList(Context context, StorageManageMatrBean.BodyBean materielItem) {
        if (materielItem.getStoragePositionList() == null || materielItem.getStoragePositionList().isEmpty()) {
            return context.getResources().getString(R.string.activity_storage_manage_matr_move_prompt);
        }
        return null;
    }

    public static boolean hasPosition(StorageManageMatrBean.BodyBean materielItem, String position) {
        List<StorageManageMatrBean.BodyBean.PositionBean> positionList = materielItem.getStoragePositionList();
        if (positionList != null && positionList.size() > 0) {
            for (int i = 0; i < positionList.size(); i++) {
                StorageManageMatrBean.BodyBean.PositionBean positionItem = positionList.get(i);
                if (positionItem.getPosition().equals(position)) {
                    return true;
                }
            }
        }
        return false;
    }

    //仓库整理基于物料整理检查剩余库存
    public static boolean hasSurplusStock(StorageManageMatrBean.BodyBean materielItem, String number) {
        String collateNumber = getStorageMaterielCollateNumber(materielItem);
        String surplusStock = ArithUtils.sub(materielItem.getQty(), collateNumber).toString();
        if (ArithUtils.sub(surplusStock, number).doubleValue() > 0) {
            return true;
        }
        return false;
    }

    public static void addPosition(Context context, String materielId, String position, String number, String unit) {
        StorageMaterielBean bean = getStorageMaterielBean(context);
        List<StorageManageMatrBean.BodyBean> materielList = bean.getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            StorageManageMatrBean.BodyBean materielItem = materielList.get(i);
            if (materielItem.getId().equals(materielId)) {
                materielList.remove(materielItem);
                StorageManageMatrBean.BodyBean.PositionBean positionBean = new StorageManageMatrBean.BodyBean.PositionBean();
                positionBean.setPosition(position);
                positionBean.setNumber(number);
                positionBean.setUnit(unit);
                List<StorageManageMatrBean.BodyBean.PositionBean> positionList = materielItem.getStoragePositionList();
                if (positionList == null) {
                    positionList = new ArrayList<>();
                }
                positionList.add(0, positionBean);
                materielItem.setStoragePositionList(positionList);
                materielList.add(0, materielItem);
                break;
            }
        }
        bean.setMaterielList(materielList);
        saveStorageMaterielBean(context, bean);
    }

    public static void deletePosition(Context context, String materielId, String position) {
        StorageMaterielBean bean = getStorageMaterielBean(context);
        List<StorageManageMatrBean.BodyBean> materielList = bean.getMaterielList();
        OUT:
        for (int i = 0; i < materielList.size(); i++) {
            StorageManageMatrBean.BodyBean materielItem = materielList.get(i);
            if (materielItem.getId().equals(materielId)) {
//                materielList.remove(materielItem);
                List<StorageManageMatrBean.BodyBean.PositionBean> positionList = materielItem.getStoragePositionList();
                if (positionList == null) {
                    positionList = new ArrayList<>();
                }
                for (int j = 0; j < positionList.size(); j++) {
                    StorageManageMatrBean.BodyBean.PositionBean positionItem = positionList.get(j);
                    if (positionItem.getPosition().equals(position)) {
                        materielList.remove(materielItem);
                        positionList.remove(positionItem);
                        materielItem.setStoragePositionList(positionList);
                        materielList.add(0, materielItem);
                        break OUT;
                    }
                }
            }
        }
        bean.setMaterielList(materielList);
        saveStorageMaterielBean(context, bean);
    }

    //获取仓库整理基于物料整理行项目概览
    public static List<OverviewAdapter.OverviewBean> getOverViewBeanWithStorageMaterielList(List<StorageManageMatrBean.BodyBean> materielList) {
        List<OverviewAdapter.OverviewBean> resultList = new ArrayList<>();
        for (int i = 0; i < materielList.size(); i++) {
            StorageManageMatrBean.BodyBean materielItem = materielList.get(i);
            if (materielItem.getStoragePositionList() != null && materielItem.getStoragePositionList().size() > 0) {
                OverviewAdapter.OverviewBean overItem = new OverviewAdapter.OverviewBean();
                overItem.setInfo(materielItem.getMat_name());
                overItem.setNumber(StorageManageUtils.getStorageMaterielCollateNumber(materielItem) + materielItem.getUnit_name());
                resultList.add(overItem);
            }
        }
        return resultList;
    }

    public static StorageManageMaterielConfirmRequestBean getStorageManageMaterielConfirmRequestBean(Context context) {
        StorageManageMaterielConfirmRequestBean result = new StorageManageMaterielConfirmRequestBean();
        StorageMaterielBean bean = getStorageMaterielBean(context);
        result.setFty_id(bean.getFactoryId());
        result.setLocation_id(bean.getInventoryAddressId());
        result.setWh_id(bean.getWarehouseId());
        //物料
        List<StorageManageMaterielConfirmRequestBean.ItemListBean> resultMaterielList = new ArrayList<>();
        List<StorageManageMatrBean.BodyBean> materielList = bean.getMaterielList();
        for (int i = 0; i < materielList.size(); i++) {
            StorageManageMatrBean.BodyBean materielItem = materielList.get(i);
            if (materielItem.getStoragePositionList() != null && materielItem.getStoragePositionList().size() > 0) {
                StorageManageMaterielConfirmRequestBean.ItemListBean resultMaterielItem = new StorageManageMaterielConfirmRequestBean.ItemListBean();
                resultMaterielItem.setId(materielItem.getId());
                resultMaterielItem.setMat_id(materielItem.getMat_id());
                resultMaterielItem.setS_area_id(materielItem.getS_area_id());
                resultMaterielItem.setS_position_id(materielItem.getS_position_id());
                List<StorageManageMaterielConfirmRequestBean.ItemListBean.TargetPositionListBean> resultPositionList = new ArrayList<>();
                List<StorageManageMatrBean.BodyBean.PositionBean> positionList = materielItem.getStoragePositionList();
                for (int j = 0; j < positionList.size(); j++) {
                    StorageManageMaterielConfirmRequestBean.ItemListBean.TargetPositionListBean resultPositionItem = new StorageManageMaterielConfirmRequestBean.ItemListBean.TargetPositionListBean();
                    StorageManageMatrBean.BodyBean.PositionBean positionItem = positionList.get(j);
                    resultPositionItem.setT_area_position_code(positionItem.getPosition());
                    resultPositionItem.setQty(positionItem.getNumber());
                    resultPositionList.add(resultPositionItem);
                }
                resultMaterielItem.setTarget_position_list(resultPositionList);
                resultMaterielList.add(resultMaterielItem);
            }
        }
        result.setItem_list(resultMaterielList);
        return result;
    }

}
